package com.cd.university.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cd.university.common.RestResult;
import com.cd.university.controller.form.LogSearchByTimeForm;
import com.cd.university.controller.form.LogTimeForm;
import com.cd.university.db.mapper.UserLoginLogMapper;
import com.cd.university.db.pojo.UserLoginLog;
import com.cd.university.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 陈栋
 * @create 2021/11/7 20:12
 */
@RestController
@RequestMapping("/log")
@Api("操作及用户日志接口")
public class LogController {

    @Autowired
    private LogService logService;

    @Autowired
    private UserLoginLogMapper userLoginLogMapper;

    @GetMapping("/getUserLog")
    @ApiOperation("查询用户登录日志")
    public RestResult getUserLog(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false,defaultValue = "6") Integer pageSize) {
        return logService.getUserLog(pageNum, pageSize);
    }

    @PostMapping("/deleteUserLog")
    @ApiOperation("批量删除用户日志")
    public RestResult deleteUserLog(@RequestBody JSONObject jsonObject) {
        JSONArray list = jsonObject.getJSONArray("userLogList");
        List<LogTimeForm> userLogList = list.toJavaList(LogTimeForm.class);

        return logService.deleteUserLog(userLogList);
    }

    @GetMapping("/getOperationLog")
    @ApiOperation("获得所有的操作日志")
    public RestResult getOperationLog(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                      @RequestParam(required = false,defaultValue = "6") Integer pageSize) {
        return logService.getOperationLog(pageNum, pageSize);
    }

    @PostMapping("/deleteOperationLog")
    @ApiOperation("批量删除操作日志")
    public RestResult deleteOperationLog(@RequestBody JSONObject jsonObject) {
        JSONArray list = jsonObject.getJSONArray("logList");
        List<LogTimeForm> operationLogList = list.toJavaList(LogTimeForm.class);

        return logService.deleteOperationLog(operationLogList);
    }

    @GetMapping("/getSystemLog")
    @ApiOperation("获得所有的系统日志")
    public RestResult getSystemLog(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false,defaultValue = "6") Integer pageSize) {
        return logService.getSystemLog(pageNum, pageSize);
    }

    @PostMapping("/deleteSystemLog")
    @ApiOperation("批量删除系统日志")
    public RestResult deleteSystemLog(@RequestBody JSONObject jsonObject) {
        JSONArray list = jsonObject.getJSONArray("logList");
        List<LogTimeForm> javaList = list.toJavaList(LogTimeForm.class);
        return logService.deleteSystemLog(javaList);
    }

    @PostMapping("/searchByTime/{type}")
    @ApiOperation("根据时间搜索")
    public RestResult searchByTime(@Valid @RequestBody LogSearchByTimeForm form,
                                   @PathVariable("type") String type) {
        return logService.searchByTime(form.getPageNum(), form.getPageSize(), form.getStartTime(), form.getEndTime(), type);
    }
}
